﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace StoveTop
{
	[Activity (Label = "InventoryScreen")]			
	public class InventoryScreen : ListActivity
	{
		StoveTop.Models.Ingredient[] items;
		List<StoveTop.Models.Ingredient> ingredients;
		StoveTopDBHelper db;

		protected override void OnCreate(Bundle bundle)
		{
			db = new StoveTopDBHelper (this);
			ingredients = db.GetAllIngredientsInInventory ();
			base.OnCreate(bundle);
			items = ingredients.ToArray();
			ListAdapter = new InventoryListAdapter(this, items);
		}
		protected override void OnListItemClick(ListView l, View v, int position, long id)
		{
			db.ClearIngredientFromInventory(items[position].GetId());
			ingredients = db.GetAllIngredientsInInventory ();
			items = ingredients.ToArray();
			ListAdapter = new InventoryListAdapter(this, items);
		}

	}

	public class InventoryListAdapter : BaseAdapter<string> {
		StoveTop.Models.Ingredient[] items;
		Activity context;
		public InventoryListAdapter(Activity context, StoveTop.Models.Ingredient[] items) : base() {
			this.context = context;
			this.items = items;
		}
		public override long GetItemId(int position)
		{
			return position;
		}
		public override string this[int position] {  
			get { return items[position].GetName(); }
		}
		public override int Count {
			get { return items.Length; }
		}
		public override View GetView(int position, View convertView, ViewGroup parent)
		{
			View view = convertView; // re-use an existing view, if one is available
			if (view == null) // otherwise create a new one
				view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, null);
			view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position].GetName();
			return view;
		}
	}
}
